package com.zsh.wanandroid.ui.activity;

import android.app.ProgressDialog;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zsh.wanandroid.R;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.LoginResult;
import com.zsh.wanandroid.bean.RegisterResult;
import com.zsh.wanandroid.common.APP;
import com.zsh.wanandroid.utils.UIUtils;

import retrofit2.Call;
import retrofit2.Response;

public class RegisterActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMore;
    private TextInputEditText etUsername;
    private TextInputEditText etPassword;
    private TextInputEditText etConfirmPassword;
    private Button btnRegister;
    private TextView tvLogin;

    @Override
    public void initPrams(Bundle prams) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        etUsername = $(R.id.etUsername);
        etPassword = $(R.id.etPassword);
        etConfirmPassword = $(R.id.etConfirmPassword);
        btnRegister = $(R.id.btnRegister);
        tvLogin = $(R.id.tvLogin);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack://返回
                back();
                break;

            case R.id.btnRegister://注册
                register();
                break;

            case R.id.tvLogin://跳转登录页面
                startActivity(LoginActivity.class);
                finish();
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText("注册");
        ivMore.setVisibility(View.GONE);
    }

    private void back() {

    }

    private void register() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("登录中...");
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();
        if (username.equals("")) {
            UIUtils.showToast("输入用户名");
            return;
        }
        if (password.equals("")) {
            UIUtils.showToast("输入密码");
            return;
        }
        if (confirmPassword.equals("")) {
            UIUtils.showToast("输入确认密码");
            return;
        }
        if (!confirmPassword.equals(password)) {
            UIUtils.showToast("输入的密码与确认密码不一致");
            return;
        }
        progressDialog.show();
        Call<RegisterResult> registerCall = APP.apiService.register(username, password, confirmPassword);
        registerCall.enqueue(new retrofit2.Callback<RegisterResult>() {
            @Override
            public void onResponse(Call<RegisterResult> call, Response<RegisterResult> response) {
                progressDialog.dismiss();
                RegisterResult registerResult = response.body();
                String errorMsg = registerResult.getErrorMsg();
                if (null != registerResult.getData()) {
                    UIUtils.showToast("注册成功！");
                } else {
                    UIUtils.showToast(errorMsg);
                }
            }

            @Override
            public void onFailure(Call<RegisterResult> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }
}
